package com.zhou.framework.util;

import com.zhou.framework.config.BlackListConfig;
import com.zhou.org.dao.BlackList;
import com.zhou.util.StringTool;
import com.zhou.org.service.BlackListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author lang.zhou
 * @date 2019/5/28
 */
@Slf4j
@Configuration
public class BlackListUtil {
    private static BlackListConfig config ;
    private static BlackListService service ;

    public BlackListUtil(BlackListConfig config,BlackListService service) {
        BlackListUtil.config = config;
        BlackListUtil.service = service;
    }

    public static BlackList getBlackList(String host){
        return config.getBlackList(host);
    }
    public static boolean inBlackList(BlackList i){
        return config.inBlackList(i);
    }
    public static boolean inBlackList(String host){
        if(StringTool.isNotBlank(host)){
            BlackList i = getBlackList(host);
            return config.inBlackList(i);
        }
        return true;
    }

    public static void putBlackItem(BlackList item){
        config.putBlackItem(item);
    }
    public static void removeBlackItem(BlackList item){
        config.removeBlackItem(item);
    }

    /**
     * 刷新黑名单缓存
     */
    public static boolean refreshCache(){
        List<BlackList> list = service.list();
        return config.refreshCache(list);
    }


}
